var forgotPwd = {
		email:"",//邮箱
		timers:	180,
		tipId: "",
		imageCode:"",
		smsCode:"",
		//初始化
		init:function(){
			//提交事件绑定
			$(".btn.btn-submit").click(function(){
				$(".form-group").removeClass("has-error");
				var formBase = $(this).parents(".form-base");
				if(formBase.hasClass("steps1")){
					forgotPwd.steps1(formBase);
				}else if(formBase.hasClass("steps2")){
					forgotPwd.steps2(formBase);
				}else if(formBase.hasClass("steps3")){
					forgotPwd.steps3(formBase);
				}
			});
			//发送短息
			$(".sendSms").click(function(){
				
				var $that = $(this);
				if($that.hasClass("disabled")){
					return false;
				}
				var errMsg = $that.parents(".form-base").find(".err-msg");
				errMsg.hide()
				$.post("/forgotPwd/sendSms",{
					email:forgotPwd.email
				},function(data){
					if(data.code=="200"){
						$that.addClass("disabled");
						var text = $that.text();
						forgotPwd.timers=180;
						forgotPwd.tipId=window.setInterval("forgotPwd.timer_('"+text+"')",1000);
					}else{
						errMsg.text(data.msg);
						errMsg.show();
						$(".sendSms").removeClass("disabled");
					}
				},"json")
				
			});
			$("#image").click(function(){
				var $that = $(this);
				$that.attr("src","/valicode?t="+new Date().getTime());
			});
		},
		//密码找回第一步
		steps1:function(formBase){
			var email =formBase.find(".emailVal");
			var imageCode =formBase.find(".imageCodeVal");
			var emailVal = email.val();
			if(''==emailVal){
				email.parents(".form-group").addClass("has-error");
				return;
			}
			var imageCodeVal = imageCode.val();
			if(''==imageCodeVal){
				imageCode.parents(".form-group").addClass("has-error");
				return;
			}
			//确认账户提交请求
			$.post("/forgotPwd/account",{email:emailVal,imageCode:imageCodeVal},function(data){
				if(data.code=='200'){
					//跳转第二步
					formBase.hide();
					$(".form-base.steps2").show();
					$("#step").attr("step","2");
					stepChange();
					forgotPwd.email = emailVal;
					//展示手机号
					$(".form-base.steps2 .mobileText").text(data.data);
					$(".sendSms").addClass("disabled");
					var text = $(".sendSms").text();
					forgotPwd.timers=180;
					forgotPwd.tipId=window.setInterval("forgotPwd.timer_('"+text+"')",1000);
				}else{
					//提示错误
					var errMsg = formBase.find(".err-msg");
					errMsg.text(data.msg);
					errMsg.show();
				}
			},'json');
		},
		//第二步
		steps2:function(formBase){
			var smsCode = formBase.find(".smsCode");
			if(smsCode.val()==''){
				smsCode.parents(".form-group").addClass("has-error");
				return;
			}
			//验证手机短信
			$.post("/forgotPwd/sms",{
				email:forgotPwd.email,
				smsCode:smsCode.val()
			},function(data){
				if(data.code=='200'){
					//跳转第三步
					formBase.hide();
					forgotPwd.smsCode = smsCode.val();
					$(".form-base.steps3").show();
					$("#step").attr("step","3");
					stepChange();
				}else{
					//提示错误
					var errMsg = formBase.find(".err-msg");
					errMsg.text(data.msg);
					errMsg.show();
				}
			},'json');
		},
		//第三步
		steps3:function(formBase){
			var pwd1 = formBase.find(".pwd1");
			var pwd2 = formBase.find(".pwd2");
			if(pwd1.val()==''){
				pwd1.parents(".form-group").addClass("has-error");
				return;
			}
			if(pwd2.val()==''){
				pwd2.parents(".form-group").addClass("has-error");
				return;
			}
			
			
			//验证手机短信
			$.post("/forgotPwd/save",{
				smsCode:forgotPwd.smsCode,
				email:forgotPwd.email,
				password:pwd1.val(),
				password1:pwd2.val()
			},function(data){
				if(data.code=='200'){
					//跳转第四步
					formBase.hide();
					$(".form-base.steps4").show();
					$("#step").attr("step","4");
					stepChange();
				}else{
					//提示错误
					var errMsg = formBase.find(".err-msg");
					errMsg.text(data.msg);
					errMsg.show();
				}
			},'json');
		},
		timer_: function(text){
			if(forgotPwd.timers>=0){
				$(".sendSms").text(forgotPwd.timers+"S");
				forgotPwd.timers--;
			}else{
				window.clearInterval(forgotPwd.tipId);
				$(".sendSms").text(text);
				$(".sendSms").removeClass("disabled");
			}
		}
}
forgotPwd.init();